<?php
namespace Admin\Controller;
use Think\Controller;
class WapmessageController extends Controller {
    
	public static $wx;//缓存微信对象
	protected function _initialize(){
		$options['appid']=C('BB_APPID');
		$options['secret']=C('BB_SECRET');
		self::$wx=new \bb\Wechat($options);		//new 微信类
		
		header("Content-Type: text/html;charset=utf-8");
	}
	
	public function index(){
		if(!strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger")){
			exit('请使用微信浏览器访问本应用！');
		}
		if(I('code') && I('openid')){
			$d=self::$wx->getOauthAccessToken();
			if($d['openid']){
				$kefu=M('weixin_kefu')->where(array('openid'=>$d['openid']))->find();
				if($kefu){
					$openid=I('openid');
					$m=M('weixin_message');
					$user=M('weixin_user');
					$_SESSION['url']=C('BB_URL');
					$nickname=$user->where(array('openid'=>$openid))->find();
					$this->assign('nickname',$nickname);
					$this->assign('openid',$openid);
					$kefus=$user->where(array('openid'=>$openid))->find();;
					$this->assign('kefu',$kefus);
					//=================
					$msg1=$m->where(array('fromusername'=>$openid))->order('createtime desc')->limit(20)->select();
					$msg2=$m->where(array('tousername'=>$openid))->order('createtime desc')->limit(20)->select();
					foreach ($msg1 as $k => $v) {
						$u=$user->where(array('openid'=>$v['fromusername']))->find();
						$msg1[$k]['nickname']=$u['nickname'];
						$msg1[$k]['headimgurl']=$u['headimgurl'];
						$up['id']=$v['id'];
						$up['is_look']=1;
						$m->save($up);
					}
					foreach ($msg2 as $k => $v) {
						$u=$user->where(array('openid'=>$v['fromusername']))->find();
						if($u){
							$msg2[$k]['nickname']=$u['nickname'];
							$msg2[$k]['headimgurl']=$u['headimgurl'];
						}else{
							$msg2[$k]['nickname']='我要洗衣';
							$msg2[$k]['headimgurl']='__PUBLIC__/admin/img/tx.png';
						}
						$up['id']=$v['id'];
						$up['is_look']=1;
						$m->save($up);
					}
					$msg=array_merge($msg1,$msg2);
					//p($msg);
					//-排序-----------------------------------------
					$sort = array('direction' => 'SORT_ASC','field'=> 'createtime');  
					$arrSort = array();  
					foreach($msg AS $uniqid => $row){
						foreach($row AS $key=>$value){  $arrSort[$key][$uniqid] = $value;  }  
					}  
					if($sort['direction']){  
						array_multisort($arrSort[$sort['field']], constant($sort['direction']), $msg);  
					}

					$this->assign('cache',$msg);
					$this->assign('sj',time());		
					$this->display();
					//==================
				}else{
					exit('没有权限访问！');
				}
			}
		}else{
			//静默授权URl
			$oa_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$url=self::$wx->getOAuthRedirect($oa_url,'xybingbing');
			header("location:".$url);
		}
	}
	
	//发送消息
	public function sendmsg(){
		$m=M('weixin_message');
		$user=M('weixin_user');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			$re=self::$wx->sendMsg($p['openid'],$p['content']);
			if($re){
				$msgsend['tousername']=$p['openid'];
				$msgsend['fromusername']=$p['toopenid'];
				$msgsend['createtime']=time();
				$msgsend['msgtype']='text';
				$msgsend['content']=$p['content'];
				$msgsend['is_look']=1;
				$m->add($msgsend);
				
				$info['status']=1;
				$info['msg']='发送成功！';
			}else{
				$info['status']=0;
				$info['msg']='发送失败！';
			}
			$this->ajaxreturn($info);
		}
	}
	
	//================================
	function getmsg(){
		$m=M('weixin_message');
		$user=M('weixin_user');
		if(IS_POST){
			$openid=I('openid');
			$msg1=$m->where(array('fromusername'=>$openid,'is_look'=>0))->order('createtime desc')->limit(20)->select();
			$msg2=$m->where(array('tousername'=>$openid,'is_look'=>0))->order('createtime desc')->limit(20)->select();
			if($msg1){
				foreach ($msg1 as $k => $v){
					$u=$user->where(array('openid'=>$v['fromusername']))->find();
					$msg1[$k]['nickname']=$u['nickname'];
					$msg1[$k]['headimgurl']=$u['headimgurl'];
					$msg1[$k]['data']=date("Y-m-d H:i:s",$v['createtime']);
					$up['id']=$v['id'];
					$up['is_look']=1;
					$m->save($up);
				}
			}
			if($msg2){
				foreach ($msg2 as $k => $v){
					$u=$user->where(array('openid'=>$v['fromusername']))->find();
					$msg2[$k]['nickname']=$u['nickname'];
					$msg2[$k]['headimgurl']=$u['headimgurl'];
					$msg2[$k]['data']=date("Y-m-d H:i:s",$v['createtime']);
					$up['id']=$v['id'];
					$up['is_look']=1;
					$m->save($up);
				}
			}
			if($msg1 || $msg2){
				$msg=array_merge($msg1,$msg2);
				//-排序-----------------------------------------
				$sort = array('direction' => 'SORT_ASC','field'=> 'createtime');  
				$arrSort = array();  
				foreach($msg AS $uniqid => $row){
					foreach($row AS $key=>$value){  $arrSort[$key][$uniqid] = $value;  }  
				}
				if($sort['direction']){  
					array_multisort($arrSort[$sort['field']], constant($sort['direction']), $msg);  
				}
			}
			
			
			if($msg){
				$info['status']=1;
				$info['data']=$msg;
				$this->ajaxreturn($info);
			}else{
				$info['status']=0;
				$this->ajaxreturn($info);
			}
		}else{
			exit('请使用post来获取数据！');
		}
	}
}